package com.jxust.hr.controller;

import com.jxust.debate.util.CommonResult;
import com.jxust.hr.service.ManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@Api(tags="manager-部门主管")
@RestController
@RequestMapping("/manager")
public class ManagerController {
    @Autowired
    ManagerService managerService;

    @ApiOperation("查看今日的待办事务")
    @ApiImplicitParam(name="username",value="用户名",required=true,dataType="String",defaultValue = "manager")
    @GetMapping(value = "/getTodos")
    public CommonResult getTodos(@RequestParam(value = "username") String username){
        return CommonResult.success("成功返回待办事务,LIMIT 3",managerService.getTodos(username));
    }

    @ApiOperation("设置待办事务")
    @ApiImplicitParams({   //方法参数的说明
            @ApiImplicitParam(name="username",value="用户名",dataType="String",defaultValue = "manager"),
            @ApiImplicitParam(name="time",value="TODO提醒时间",dataType="String",defaultValue = "2021-01-27"),
            @ApiImplicitParam(name="tag",value="标签,提醒的类型",dataType="String",defaultValue = "其他"),
            @ApiImplicitParam(name="todo",value="需要提醒的内容",dataType="String",defaultValue = "完成XXXXXXX")
    })
    @PostMapping(value = "/setTodo")
    public CommonResult setTodo(@RequestParam(value = "username") String username,
                                @RequestParam(value = "time") String time,
                                @RequestParam(value = "tag") String tag,
                                @RequestParam(value = "todo") String todo){
        String msg="设置成功, 前端date时间转换==> let d=new Date(date);  let time=d.getFullYear().toString().padStart(4, \"0\") + '-' + (d.getMonth().toString().padStart(2, \"0\") + 1) + '-' + d.getDate().toString().padStart(2, \"0\") + ' ' +\n";
        int result=   managerService.setTodo(username,time,tag,todo);
        if(result==1){
            return  CommonResult.success("插入成功,"+msg);
        }else {
            return  CommonResult.fail("插入失败,"+msg);
        }
    }




    @ApiOperation("查看本公司的总体出勤情况")
    @GetMapping(value = "/selectAttendanceCount")
    public CommonResult selectAttendanceCount(){
        return CommonResult.success("返回基本信息包括: 员工总人数, 迟到,缺勤,早退",managerService.selectAttendanceCount());
    }

    @ApiOperation("查看本周个人的出勤状况")
    @GetMapping(value = "/selectAttendanceInfo")
    public CommonResult selectAttendanceInfo(){

        return CommonResult.success("查询限制为5条",managerService.selectAttendanceInfo());
    }

    @ApiOperation("得到所有的职位")
    @GetMapping(value = "/getPositions")
    public CommonResult getPositions(){

        return CommonResult.success("成功",managerService.getPositions());
    }

    @ApiOperation("根据用户名或者职位搜索对应的员工集合")
    @ApiImplicitParams({   //方法参数的说明
            @ApiImplicitParam(name="name",value="查询的员工名",dataType="String",defaultValue = ""),
            @ApiImplicitParam(name="position",value="查询的职位名",dataType="String",defaultValue = "全部"),
            @ApiImplicitParam(name="limit",value="每页显示的记录行数",dataType="Integer",defaultValue = "4"),
            @ApiImplicitParam(name="currentPage",value="当前第几页",dataType="Integer",defaultValue = "1")
    })
    @GetMapping(value = "/getMembers")
    public CommonResult getMembers(@RequestParam(value = "name", defaultValue = "") String name,
                                   @RequestParam(value = "position", defaultValue = "全部") String position,
                                   @RequestParam(value = "limit", defaultValue = "4") Integer limit,
                                   @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage){

        //每页的起始行(offset+1)数据，如第一页(offset=0，从第1(offset+1)行数据开始)
        int offset = (currentPage - 1)*limit;
        return CommonResult.success("成功返回,第"+offset+"页数据,每页有"+limit+"行数据",
                managerService.getMembers(name,position,limit,offset));
    }



    @ApiOperation("通过员工ID查找员工的'全面'信息")
    @ApiImplicitParam(name="employeeId",value="员工ID",dataType="String",defaultValue = "1")
    @GetMapping(value = "/getEmployeeInfoAll")
    public CommonResult getEmployeeInfoAll(@RequestParam(value ="employeeId")String employeeId){
        return CommonResult.success("成功返回,员工ID为"+employeeId+"的详细信息",
                managerService.getEmployeeInfoAll(employeeId));
    }


    @ApiOperation("获取员工的姓名和ID的List集合")
    @GetMapping(value = "/getEmployeeList")
    public CommonResult getEmployeeList(){
        return  CommonResult.success("成功返回,员工ID和其姓名 的集合 LIMIT 10",
                managerService.getEmployeeList());
    }



    @ApiOperation("分配任务")
    @ApiImplicitParams({   //方法参数的说明
            @ApiImplicitParam(name="task",value="任务描述",required=true,dataType="String",defaultValue = "开发项目"),
            @ApiImplicitParam(name="start_time",value="开始时间",required=true,dataType="String",defaultValue = "2021-1-5 13:22:31"),
            @ApiImplicitParam(name="end_time",value="结束时间",required=true,dataType="String",defaultValue = "2021-1-27 13:22:33"),
            @ApiImplicitParam(name="executors",value="分配到任务的员工,JSON.stringify() ,[\"赵琳浩\",\"鹿存亮\",\"江南\"] ",required=true,dataType="String",defaultValue = "[\"1355\",\"13\",\"12\",\"11\"]")
    })
    @PostMapping(value = "/setTasks")
    public CommonResult setTasks(@RequestParam(value ="task")String task,
                                 @RequestParam(value ="start_time")String start_time,
                                 @RequestParam(value ="end_time")String end_time,
                                 @RequestParam(value ="executors") String executors){
/*        JSONArray jsonArray=new JSONArray(executors);//json的字符串数组转集合
        List<String> list=(List)jsonArray;*/
        int result=managerService.setTasks(task,start_time,end_time,executors);
        if(result==1){
            return  CommonResult.success("插入成功");
        }else {
            return  CommonResult.fail("插入失败");
        }

    }


    @ApiOperation("得到任务")
    @GetMapping(value = "/getTasks")
    public CommonResult getTasks(){
        return CommonResult.success("成功返回数据,得到的executors是String类型使用JSON.parse()还原成数组",
                managerService.getTasks());
    }

    @ApiOperation("设置任务的完成度")
    @ApiImplicitParams({   //方法参数的说明
            @ApiImplicitParam(name="id",value="任务的id",required=true,dataType="Integer",defaultValue = "1"),
            @ApiImplicitParam(name="completion",value="完成度",required=true,dataType="String",defaultValue = "0%"),
    })
    @PostMapping(value = "/setTaskCompletion")
    public CommonResult setTaskCompletion(@RequestParam(value ="id")Integer id,
                                          @RequestParam(value ="completion")String completion) {
        int result =managerService.setTaskCompletion(id,completion);
        if(result==1){
            return  CommonResult.success("插入成功");
        }else {
            return  CommonResult.fail("插入失败");
        }
    }

    @ApiOperation("得到评价页面里的员工信息")
    @GetMapping(value = "/getEvaluationList")
    public CommonResult getEvaluationList(){
        return CommonResult.success("成功返回数据",
                managerService.getEvaluationList());
    }

    @ApiOperation("设置员工的评价")
    @ApiImplicitParams({   //方法参数的说明
            @ApiImplicitParam(name="employeeId",value="员工的id",required=true,dataType="String"),
            @ApiImplicitParam(name="working_performance",value="工作表现",required=true,dataType="String"),
            @ApiImplicitParam(name="violation",value="违纪现象",required=true,dataType="String"),
            @ApiImplicitParam(name="contribution",value="贡献",required=true,dataType="String"),
            @ApiImplicitParam(name="others",value="其他意见",required=true,dataType="String"),
    })
    @PostMapping(value = "/setEvaluation")
    public CommonResult setEvaluation(@RequestParam(value ="employeeId")String employeeId,
                                      @RequestParam(value ="working_performance")String working_performance,
                                      @RequestParam(value ="violation")String violation,
                                      @RequestParam(value ="contribution")String contribution,
                                      @RequestParam(value ="others")String others){

        int  result =  managerService.setEvaluation(employeeId,working_performance,violation,contribution,others);
        if(result==1){
            return  CommonResult.success("插入成功");
        }else {
            return  CommonResult.fail("插入失败");
        }

    }


    @ApiOperation("通过id获取 员工档案信息")
    @ApiImplicitParam(name="employeeId",value="员工的id",required=true,dataType="String")
    @GetMapping(value = "/getEmployeeCompanyById")
    public CommonResult getEmployeeCompanyById(@RequestParam(value ="employeeId")String employeeId){
        return  CommonResult.success("成功返回,员工档案信息: 员工个人信息,员工当前公司,和以前的公司",
                managerService.getEmployeeCompanyById(employeeId));
    }





}
